$(function() {
	// 页面控件
	var $dataTable = $('#dataTable'); // 表格
	var $dataTableToolbar = $('#dataTableToolbar'); // 表格：工具栏
	var $cachedTime = $('#cachedTime');
	
	var $type = $('#type');
	var $shelfId = $('#shelfId');
	
	initWidgets();
	initListeners();
	
	$('#btnExport').on('click', function() {
		alert("逗你玩");
	});
	
	function refreshCachedTime() {
		$.ajax({
			url: '../../time',
			type: 'GET',
			dataType: 'json',
			contentType: "application/json; charset=utf-8",
			success: function(result) {
				if (result.success) {
					$cachedTime.text(result.data);
				}
			}
		});
	}
	
	function getHeight() {
	    return $(window).height() - 40;
	}
	
	// 初始化事件监听
	function initListeners() {
		$(window).resize(function () {
			$dataTable.bootstrapTable('resetView', {
	            height: getHeight()
	        });
	    });
	}
	
	function initWidgets() {
		refreshCachedTime();
		buildMinerTable();
	}
	
	function buildMinerTable() {
		var type = $type.val();
		var shelfId = $shelfId.val();
		
		var urlStr = "../../minerDisplays/"+type+"/"+shelfId;
			
		//alert(urlStr);
		$dataTable.bootstrapTable({
			height: getHeight(),
			url: urlStr,
			pagination: true,
			sidePagination: 'server',
			pageSize: 100,
			pageList: [10, 25, 50, 100],
			showRefresh: true,
			toolbar: '#dataTableToolbar',
			iconSize: 'outline',
			icons: {
				paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
	            paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
	            refresh: 'glyphicon-repeat',
				toggle: 'glyphicon-list-alt',
				columns: 'glyphicon-list',
	            detailOpen: 'glyphicon-plus icon-plus',
	            detailClose: 'glyphicon-minus icon-minus'
			},
			columns: [
					{
						field: 'minerShelfId',
						title: '机架',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'minerMac',
						title: 'MAC',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'minerHost',
						title: 'IP',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'minerType',
						title: '机型',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'firmwareVersion',
						title: '固件版本',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'elapsed',
						title: '运行时间',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'poolUser',
						title: '矿工',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'poolUrl',
						title: '矿池',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'hashReal',
						title: '5S算力',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'hashAvg',
						title: '平均算力',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'fanNum',
						title: '风扇数',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'fanMaxSpeed',
						title: '最高转速',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'tempNum',
						title: '温感数',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'tempMaxValue',
						title: '最高温度',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'chainNum',
						title: '链条数',
						halign: 'center',
						align: 'center',
					},
					{
						field: 'chipNum',
						title: '芯片数',
						halign: 'center',
						align: 'center',
					}
			]
		});
	}
	
	
});